package com.crazymaker.springcloud.base.security.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OptionsRequestFilter extends OncePerRequestFilter
{

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException
    {

        if (request.getMethod().equals("OPTIONS" ))
        {
            response.setHeader("Access-Control-Allow-Origin", "*" );
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS,HEAD,DELETE" );
            response.setHeader("Access-Control-Max-Age", "3600" );
            response.setHeader("Access-Control-Allow-Headers",
                    "Content-Type,x-requested-with,X-Custom-Header,Authorization,token,backend" );

            return;
        }
        filterChain.doFilter(request, response);
    }

}
